package m.jclecai.com.base.digitalLottery;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.adapter.klsfRecyclerAdapter.HistoryAdapter;
import m.jclecai.com.bean.IssueInfoBean;
import m.jclecai.com.bean.MissDataBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.listener.Callback;
import m.jclecai.com.utils.BaseUtils;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;
import m.jclecai.com.view.common.BottomBar;
import m.jclecai.com.view.common.VeticalDrawerLayout;
import q.rorbin.badgeview.Badge;

/**
 * Created by andy on 2017/7/6.
 * <p>
 * 这里是数字彩票的一个默认的基础父类主要包含：
 * 快乐十分，大乐透，福彩3d,时时彩......
 */


/**
 * Created by andy on 2017/7/6.
 * <p>
 * 这里是数字彩票的一个默认的基础父类主要包含：
 * 快乐十分，大乐透，福彩3d,时时彩......
 */

public abstract class DefaultDigitalLotteryActivity extends BaseDigitalLotteryActivity {

    //这里是显示金额的；
    @BindView(R.id.tv_number_money)
    protected TextView mTvNumberMoney;          //注数和金额

    //这里是表题上面的日期的；
    @BindView(R.id.tv_term)
    protected TextView mTvTerm;       //期数
    @BindView(R.id.tv_date)
    protected TextView mTvDate;       //时间，什么时候截止....
    @BindView(R.id.ll_date)
    protected LinearLayout mLlDate;   //期数和时间的一个LinearLayout
    @BindView(R.id.iv_date_unfold)
    protected ImageView mIvDateUnfold;    //期数左边的那个键头；

    //历史的开奖信息
    @BindView(R.id.rv_recycler_view)
    protected RecyclerView mRvHistory;       //历史的记录；

    @BindView(R.id.bottom_bar)
    BottomBar mBottomBar;           //底部的确认区
    HistoryAdapter mHistoryAdapter;
    protected String mType = "";


    @Override
    protected int getNumberMoneyRes() {
        return R.layout.layout_digital_lottery_number_money;
    }

    @Override
    protected int getBottomRes() {
        return R.layout.layout_digital_lottery_bottom;
    }

    @Override
    protected int getHeaderRes() {
        return R.layout.term_date;
    }

    @Override
    protected int getInnerRes() {
        return R.layout.template_recycler_view;
    }


    @Override
    protected void initView() {
        super.initView();
        initBottomView();
        initHeaderView();
        initDrawerLayout();
        initHistory();
    }

    private void initHistory() {
        mHistoryAdapter = new HistoryAdapter(null);
        mRvHistory.setLayoutManager(new LinearLayoutManager(this));
        mRvHistory.setAdapter(mHistoryAdapter);
    }

    protected void initDrawerLayout() {
        mDrawerLayout.setOnDragStatusChange(new VeticalDrawerLayout.OnDragStatusChange() {
            @Override
            public void onOpen() {

            }

            @Override
            public void onClose() {

            }

            @Override
            public void OnDrag(float percent) {
                onVeticalDrawerLayoutDrag(percent);
            }
        });
    }

    /**
     * 当垂直方向的抽屉进行一个拖动的时候，一个回掉方法；
     *
     * @param percent 移动的一个百分比
     */
    protected void onVeticalDrawerLayoutDrag(float percent) {
        //这里我们的一个默认的方法就是显示header上面的一个箭头
        if (percent >= 0 && percent <= 1) {
            Float evaluate = BaseUtils.evaluate(percent, 0, 180);
            mIvDateUnfold.setRotation(evaluate);
        }
    }

    private void initHeaderView() {
        mIvDateUnfold.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDrawerLayout.toggle();
            }
        });
    }

    protected void initBottomView() {
        mBottomBar.setLeftButtonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBottonLeftButtonClick();
            }
        });

        mBottomBar.setRightButtonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBottomRightButtonClick();
            }
        });

        mBottomBar.setOnDragstateChangeListener(new Badge.OnDragStateChangedListener() {
            @Override
            public void onDragStateChanged(int dragState, Badge badge, View targetView) {
                if (dragState == Badge.OnDragStateChangedListener.STATE_SUCCEED) {
                    //这里表示小红点被清除的事件；
                    onBottonCircleRedDragged();
                }
            }
        });
        mBottomBar.setCenterButtonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addBettingArea();
            }
        });
    }

    /**
     * 数字彩共有
     * 获取遗漏和历史数据；
     */
    protected void requestMissData(final Callback callback) {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("type", mType);
        HttpManager.postAsyn(Constant_Url.GET_MISS_DATA, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                DisPlay("网络异常！");
            }

            @Override
            public void onResponse(String response) {
                try {
                    MissDataBean bean = FastOk.get(response, MissDataBean.class);
                    if (bean != null) {
                        if (callback != null) callback.callback(bean);
                        if (mHistoryAdapter != null) mHistoryAdapter.setType(mType);
                        if (mHistoryAdapter != null) mHistoryAdapter.refresh(bean.getCodes());
                        if (mDrawerLayout != null) mDrawerLayout.measureInnerLayout();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, paramsMap);
    }

    /**
     * 数字彩共有
     * 这显示多少期，以及还剩多长时间；
     *
     * @param bean
     */
    private void setIssueInfoData(IssueInfoBean bean) {
        String termNo = bean.getTermNo();         //当前多少期；
        String deadLine = bean.getDeadLine();    //截止时间；

        String term = termNo.substring(termNo.length() - 2);
        if (mType.equals("bjkl8") || mType.equals("pk10"))
            term = termNo;
        mTvTerm.setText("距" + term + "期截止");

        long deadline = 0;
        try {
            deadline = Long.parseLong(deadLine);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        startCountDown(deadline * 1000);
    }

    /**
     * 数字彩共有
     * 截止期倒计时
     *
     * @param time
     */
    private void startCountDown(long time) {
        BaseUtils.startTermTimer(time, 1000, new Callback() {
            @Override
            public void onTimerFinish() {
                super.onTimerFinish();
                //重新获取信息；
                requestIssueInfo();
            }

            @Override
            public void onTimerTick(long millisUntilFinished) {
                super.onTimerTick(millisUntilFinished);
                //这里的话， 我们对毫秒进行一个格式化的操作；
                mTvDate.setText(BaseUtils.formatDateLineToString(millisUntilFinished));
            }
        });
    }

    /**
     * 数字彩共有
     * 更新期号信息
     */
    protected void requestIssueInfo() {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("type", mType);

        HttpManager.postAsyn(Constant_Url.ISSUE_INFO, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                DisPlay("网络异常！");
                e.printStackTrace();
            }

            @Override
            public void onResponse(String response) {
                try {

                    IssueInfoBean bean = FastOk.get(response, IssueInfoBean.class);
                    if (bean != null) {
                        setIssueInfoData(bean);
                    } else {
                        DisPlay("网络异常！");
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, paramsMap);

    }

    /**
     * 获取左边的机选按钮的文字；
     *
     * @return
     */
    public String getBottomBarleftText() {
        return mBottomBar.getLeftText();
    }

    public void setBottomBarLeftText(String text) {
        mBottomBar.setLeftButtonText(text);
    }

    /**
     * 设置投注的注数,直接显示在小红点上面的数字
     *
     * @param bettingNumber
     */
    public void setBottomBarBettingNumber(int bettingNumber) {
        mBottomBar.setBettingNumber(bettingNumber);
    }


    /**
     * 添加到投注区
     */
    protected abstract boolean addBettingArea();


    /**
     * 底部中间的小红点被拖拽的事件
     */
    protected abstract void onBottonCircleRedDragged();

    /**
     * 底部的右边按钮的点击事件
     */
    protected abstract void onBottomRightButtonClick();

    /**
     * 底部左边的按钮的点击事件；
     */
    protected abstract void onBottonLeftButtonClick();
}
